//package com.ccloud.blog.modules.elasticsearch.controller;
//
//import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
//import com.ccloud.blog.modules.blog.domain.Blog;
//import com.ccloud.blog.modules.blog.service.IBlogService;
//import com.ccloud.blog.modules.elasticsearch.domain.EsBlog;
//import com.ccloud.blog.modules.elasticsearch.mapper.BlogRepository;
//import com.ccloud.blog.modules.elasticsearch.service.EsBlogService;
//import com.ccloud.common.core.reqres.response.R;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.data.elasticsearch.core.completion.Completion;
//import org.springframework.web.bind.annotation.DeleteMapping;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.annotation.Resource;
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
//
//@RestController
//@Slf4j
//@RequestMapping("/esBlog")
//public class EsBlogController {
//
//    public static final String TITLE = "title";
//    //分页参数
//    Integer PAGE_SIZE = 10;
//
//    //默认页码
//    Integer DEFAULT_PAGE_NUMBER = 1;
//
//    @Resource
//    private IBlogService blogService;
//
//    @Resource
//    private EsBlogService esBlogService;
//
//    @Resource
//    private BlogRepository blogRepository;
//
//
//    @DeleteMapping(value = "/deleteAllIndex")
//    public R deleteAllIndex() {
//        log.info(">>>>>>即将清空博客索引...");
//        blogRepository.deleteAll();
//        log.info(">>>>>>博客索引已清空");
//        return R.ok(null, "博客索引已清空");
//    }
//
//    @RequestMapping(value = "/bulkEsBlogIndex")
//    public R bulkEsBlogIndex() {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        List<Blog> list = blogService.getAllPublishedBlogs();
//        List<EsBlog> esList = new ArrayList<>();
//        list.forEach(blog -> {
//            List<String> suggestList = new ArrayList<>();
//            suggestList.add(blog.getIntro());
//            Completion completion = new Completion(suggestList.toArray(new String[suggestList.size()]));
//            EsBlog esBlog = new EsBlog(blog.getId().toString(),
//                    blog.getTitle(),
//                    blog.getIntro(),
//                    blog.getCoverUrl(),
//                    sdf.format(blog.getCreateTime()),
//                    sdf.format(new Date()),
//                    completion);
//            esList.add(esBlog);
//        });
//        if (CollectionUtils.isNotEmpty(esList)) {
//            log.info(">>>>>>即将初始化博客索引...");
//            blogRepository.saveAll(esList);
//            log.info(">>>>>>博客索引初始化成功!");
//            return R.ok(null, "初始化博客索引到elasticsearch服务器成功");
//        } else {
//            log.error("没有找到博客内容，无法建立任何索引");
//            return R.fail("没有找到博客内容，无法建立索引");
//        }
//    }
//
//    @GetMapping(value = "getEsBlogs")
//    public R getEsBlogs(Integer pageNumber, Integer pageSize, String searchContent) {
//
//        //validate1：  校验分页参数
//        if (pageSize == null || pageSize <= 0) {
//            pageSize = PAGE_SIZE;
//        }
//
//        if (pageNumber == null || pageNumber < DEFAULT_PAGE_NUMBER) {
//            pageNumber = DEFAULT_PAGE_NUMBER;
//        }
//
//        List<EsBlog> esBlogs = esBlogService.searchEsBlogList(pageNumber, pageSize, searchContent);
//        return R.ok(esBlogs, "获取es中的博客信息成功");
//    }
//
//    @GetMapping(value = "getSuggestEsBlogTitle")
//    public R getEsBlogs(String suggestValue) {
//        List<String> suggest = esBlogService.getSuggestList("suggest", suggestValue, 10);
//        return R.ok(suggest, "获取es推荐成功");
//    }
//
//
//}
